<?php require VIEWPATH . ADMIN_DIR . DIRECTORY_SEPARATOR . 'sub' . DIRECTORY_SEPARATOR .'header.php'?>
<style>
    .cate-type {
        color: #438EB9;
        border: 1px solid #438EB9;
        padding: 1px 3px;
    }
    .recommend {
        color: #FE9B01;
        border: 1px solid #FE9B01;
        padding: 1px 20px;
    }
    .outer-table {
        margin: 20px 200px;
    }
    .clearfix {
        padding: 30px;
        text-align: center;
    }
    .btn-save {
        width:110px;
        height:40px;
        color: #FFF;
        background:#438EB9;
        border-radius:4px;
        font-size:16px;
        border: none !important;
        margin-right: 100px;
    }
    a.back {
        margin-right: 40px;
        color: #438EB9;
        font-size: 16px;
        display: flex;
        align-items: center;
    }
    a.back i {
        font-size: 25px;
    }
    .ant-card-head-title {
        text-align: center;
        margin-right: 15%;
    }
</style>
<div class="ant-card page-wrap com-card static-page___2-4kT">
    <div class="ant-card-head">
        <div class="ant-card-head-wrapper">
            <div><a class="back" href="<?= admin_url('sub/cms/category') ?>"><i class="fa fa-angle-left"></i>&nbsp;&nbsp;返回</a></div>
            <div class="ant-card-head-title">CMS栏目管理</div>
        </div>
    </div>
    <div class="outer-table">
        <form class="form-horizontal" id="category-form">
			<input type="hidden" name="id" value="<?=$categoryInfo['id']?>" />
			<input type="hidden" name="category_article_cover" value="<?=$categoryInfo['category_article_cover']?>" />

			<div class="form-group">
				<label class="col-sm-3 control-label no-padding-right">栏目名称</label>
				<div class="col-sm-6">
					<input class="form-control" type="text" placeholder="请填写栏目名称" name="category_name" value="<?=$categoryInfo['category_name']?>" />
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label no-padding-right">栏目排序</label>
				<div class="col-sm-6">
					<input class="form-control" type="text" placeholder="请填写栏目排序" name="category_sort" value="<?=$categoryInfo['category_sort']?>" />
				</div>
			</div>
			<?php if(!$categoryInfo): ?>
				<div class="form-group">
					<label class="col-sm-3 control-label no-padding-right">上级栏目</label>
					<div class="col-sm-6">
						<select class="form-control" name="parent_id">
							<option value="0">顶级栏目</option>
							<?php foreach($categoryList as $category): ?>
								<option value="<?=$category['id']?>"><?=$category['category_name']?></option>
							<?php endforeach; ?>
						</select>
					</div>
				</div>
			<?php endif; ?>
			<div class="form-group">
				<label class="col-sm-3 control-label no-padding-right">栏目推荐</label>
				<div class="col-sm-6">
					<select class="form-control" name="category_recommend">
						<option value="0">否</option>
						<option value="1">推荐</option>
					</select>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label no-padding-right">栏目类型</label>
				<div class="col-sm-6">
					<select class="form-control" name="category_type">
						<option value="<?=CMS_CATEGORY_NORMAL?>">普通栏目</option>
						<option value="<?=CMS_CATEGORY_ARTICLE?>">文章类型</option>
					</select>
				</div>
			</div>
			<div id="category-article-div" class="hide">
				<div class="form-group">
					<label class="col-sm-3 control-label no-padding-right">文章封面</label>
					<div class="col-sm-6" style="margin-top: 10px;">
						<?php if($categoryInfo['category_article_cover']): ?>
							<a href="javascript:;" id="upload-img">
								<img class="fit-cover" src="<?=$categoryInfo['category_article_cover']?>" width="160" height="196" />
							</a>
						<?php else: ?>
							<a href="javascript:;" id="upload-img">
								<img class="fit-cover" src="<?=$staticAdminUrl?>sub/assets/img/cms/add.png" width="160" height="196" />
							</a>
						<?php endif; ?>
					</div>
				</div>
				<div class="form-group">
					<label class="col-sm-3 control-label no-padding-right">文章内容</label>
					<div class="col-sm-6">
						<textarea class="pd0" id="content" name="category_article_content" style="width: 100%;"><?=urldecode($categoryInfo['category_article_content'])?></textarea>
					</div>
				</div>
			</div>
				
			<div class="clearfix">
				<div class="">
					<button class="btn-save" type="button">
						提交
					</button>
				</div>
			</div>
		</form>
    </div>
</div>
<?php require VIEWPATH . ADMIN_DIR . DIRECTORY_SEPARATOR . 'sub' . DIRECTORY_SEPARATOR .'footer.php'?>
<!-- 百度编辑器 -->
<script src="<?=$staticAdminUrl?>assets/ueditor/ueditor.config.js"></script>
<script src="<?=$staticAdminUrl?>assets/ueditor/ueditor.all.js"></script>
<script>
	var ue;

    $(function(){
        // 切换栏目类型
        $("[name=category_type]").change(function(){

            var categoryType = $(this).val();
            if(categoryType == <?=CMS_CATEGORY_ARTICLE?>) {
                show_article_type();
            } else {
                $("#category-article-div").addClass("hide");
            }
        })

        // 初始化编辑器
        ue = UE.getEditor('content');

        // 修改栏目信息
        <?php if($categoryInfo): ?>
            $("[name=category_recommend]").val("<?=$categoryInfo['category_recommend']?>");
            // 显示栏目类型
            $("[name=category_type]").val("<?=$categoryInfo['category_type']?>")
            <?php if($categoryInfo['category_type'] == CMS_CATEGORY_ARTICLE): ?>
                show_article_type();
            <?php endif; ?>
        <?php endif; ?>

        // 提交表单
        var form = $("#category-form");
        var submitUrl = "<?=admin_url('cms/save_category_action')?>";


        // 点击提交按钮
        $(".btn-save").click(function(){
            $("[name=category_article_content]").val(ue.getContent());
            form.submit();
        })
        // 提交表单
        form_submit(form, submitUrl, function(res) {
            tips_alert(res.msg, res.success, function(){
                if(res.success) window.location.href = "<?=admin_url('sub/cms/category')?>"
            });
        })
    })

    // 显示文章类型
    function show_article_type(){
        $("#category-article-div").removeClass("hide");

        // 设置高度
        setTimeout(function(){
            ue.setHeight(400);
        }, 300)

        // 上传图片对象
        $("#upload-img").uploader({
            "server":  '<?=UP_FORM_URL?>',
            "authorization": "<?=$signature?>",
            "policy": "<?=$policy?>",
            "callback": function(response) {
                var imgFile = "<?=UP_URL?>" + response.url;
                $("#upload-img").find("img").attr("src", imgFile);
                $("[name=category_article_cover]").val(imgFile);
            }
        });
    }
</script>